package com.lsz.deport.ui.fragment;

import androidx.annotation.NonNull;

import com.common.config.Global;
import com.common.config.MyApi;
import com.common.event.OrderCancelEvent;
import com.common.retrofit.RxObserver;
import com.common.retrofit.json.Data;
import com.lsz.deport.json.ProductChoiceBean;
import com.util_code.base.mvp.RxMvpPresenter;
import com.util_code.utils.RxUtils;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import retrofit2.http.Path;
import retrofit2.http.Query;

public class OrderFmtPresent extends RxMvpPresenter<OrderFmtView> {

    MyApi myApi;

    @Inject
    public OrderFmtPresent(MyApi myApi) {
        this.myApi = myApi;
    }

    void getOrderList(int pageSize, int pageNum, int status) {
        myApi.getOrderList(pageSize, pageNum, status)
                .compose(RxUtils.<Data<ProductChoiceBean>>applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<ProductChoiceBean>>() {

                    @Override
                    public void onNext(@NonNull Data<ProductChoiceBean> data) {
                        if (checkJsonCode(data, true) && getView() != null) {
                            if (data.data == null) {
                                getView().onSuccess("data");
                            } else {
                                getView().onSuccess(data.data);
                            }
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        if (getView() != null) {
                            getView().onFail();
                        }
                    }
                });
    }

    void putOrderStatus(String id, int status) {
        myApi.putOrderStatus(id, status)
                .compose(RxUtils.<Data>applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data>() {

                    @Override
                    public void onNext(@NonNull Data data) {
                        if (data.success) {
                            EventBus.getDefault().post(new OrderCancelEvent());
                            getView().onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        getView().onFail();
                    }
                });
    }

}
